//*****************************************************************************
//
//								Monitor_CPU.c
//
//*****************************************************************************
//
//
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "printf.h"
#include "CPU.h"
#include "SPI.h"
#include "Global.h"
#include "NVMData.h"
#include "util.h"
#include "monitor_SPI.h"


//=============================================================================
//
//=============================================================================
#if 0
void MonitorSPI(void)
{
	BYTE i, j, c;

	//---------------------- Dump SPI --------------------------
	if( !stricmp( argv[1], "D" ) ) {
		static DWORD spiaddr = 0;
		BYTE *ptr = SPI_Buffer;
		DWORD cnt = 0x80L;

		if( argc>=3 ) spiaddr = a2h( argv[2] );

		Printf("\nDump SPI %06lx", spiaddr);

		SPI_ReadData2xdata( spiaddr, ptr, cnt );

		for (j=0; j<8; j++) {
			Printf("\nSPI %06lx: ", spiaddr + j*0x10);
			for(i=0; i<8; i++) Printf("%02bx ", SPI_Buffer[j*0x10+i] );
			Printf("- ");
			for(; i<16; i++) Printf("%02bx ", SPI_Buffer[j*0x10+i] );
			Printf("  ");
			for(i=0; i<16; i++) {
				c = SPI_Buffer[j*0x10+i];
				if( c>=0x20 && c<0x80 ) Printf("%c", c);
				else Printf(".");
			}
		}
		spiaddr += 0x80L;
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "Sector" ) ) {
		static DWORD spiaddr = 0x10000L;

		if( argc>=3 ) spiaddr = a2h( argv[2] );

		Printf("\nSector Erase = %06lx", spiaddr);

		SPI_WriteEnable();
		SPI_SectorErase(spiaddr);
		spiaddr += 0x1000L;
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "Block" ) ) {
		static DWORD spiaddr = 0x10000L;

		if( argc>=3 ) spiaddr = a2h( argv[2] );

		Printf("\nBlock Erase = %06lx", spiaddr);

		SPI_WriteEnable();
		SPI_BlockErase(spiaddr);
		spiaddr += 0x1000L;
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "Copy" ) ) {
		DWORD source = 0x0L, dest = 0x10000L;
		DWORD cnt=0x100L;

		if( argc<4 ) {
			Printf("\nMissing Parameters !!!" );
			return;
		}
		source = a2h( argv[2] );
		dest   = a2h( argv[3] );
		cnt    = a2h( argv[4] );

		Printf("\nSPI copy from %06lx to %06lx", source, dest);
		Printf("  %03x bytes", (WORD)cnt);

		SPI_ReadData2xdata( source, SPI_Buffer, cnt );
		SPI_WriteEnable();
		SPI_PageProgram( dest, SPI_Buffer, cnt );
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "rmode" ) ) {
		BYTE mod = SPI_SetModeByRegister();
		Printf("\nSPI Read Mode = %bd", mod );
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "?" ) ) {
		Printf("\n\n  === Help for SPI command ===");
		Printf("\nSPI D [addr]      ; dump");
		Printf("\nSPI sector [addr] ; sector erase");
		Printf("\nSPI block [addr]  ; block erase");
	   	Printf("\nSPI copy [src] [dst] [cnt] ; copy");
		Printf("\n");
	}
	//--------------------------------------------------------
	else
		Printf("\nInvalid command...");	
}
#endif
//=============================================================================
//
//=============================================================================
//	Format is needed only once
//	Init is needed when starting program

void MonitorNVM(void)
{
	BYTE index, dat, i, j;

	index = a2h( argv[2] );
	dat   = a2h( argv[3] );

	//--------------------------------------------------------
	if( !stricmp( argv[1], "format" ) ) {
		Printf("\nFormat NVMPROM...");
		NVM_Format();
		return;
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "init" ) ) {
		Printf("\nInit NVMPROM variables...");
		NVM_Init();
		return;
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "W" ) ) {
		if( argc==4 ) {
			Printf("\nWrite NVMPROM %02bx:%02bx ", index, dat );
			NVM_Write( index, dat );
			dat = NVM_Read( index );
			Printf(" ==> Read NVMPROM[%02bx] = %02bx ", index, dat );
		}
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "R" ) ) {
		if( argc==3 ) {
			dat = NVM_Read( index );
			Printf("\n ==> Read NVMPROM[%02bx] = %02bx ", index, dat );
		}
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "D" ) ) {
		Printf("\nDump NVMPROM");
		for(j=0; j<2; j++) {
			Printf("\nNVMPROM %02bx:", j*0x10);
			for(i=0; i<8; i++) Printf(" %02bx", NVM_Read( j*16 + i ) );
			Printf("-");
			for(; i<16; i++) Printf("%02bx ", NVM_Read( j*16 + i ) );
		}
	}
	//--------------------------------------------------------
	else if( !stricmp( argv[1], "?" ) ) {
		Printf("\n\n  === Help for NVM command ===");
		Printf("\nNVM format         ; format and initialize");
		Printf("\nNVM init           ; initialze internal variables");
		Printf("\nNVM w [idx] [dat]  ; write data");
	   	Printf("\nNVM r [idx]        ; read data");
		Printf("\nNVM d              ; dump all data");
		Printf("\n");
	}

	else
		Printf("\nInvalid command...");	
	
}










